package com.haidao.admin.component.web.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.haidao.admin.common.utils.FieldUtils;
import com.haidao.admin.component.entity.UserOperation;
import com.haidao.admin.component.params.ReportParam;
import com.haidao.admin.component.service.ReportService;
import com.haidao.admin.component.service.UploadFileService;
import com.haidao.admin.component.service.UserOperationService;
import com.haidao.admin.core.result.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/report")
@RestController
public class ReportController {
    @Autowired
    private UploadFileService uploadFileService;



    // 表明
    // id
    // 操作
    @PostMapping("/{tableName}/{id}/{action}")
    public String report(@PathVariable("tableName") String tableName,
                         @PathVariable("id") Integer id,
                         @PathVariable("action") String action) {
        ReportService bean = SpringUtil.getBean(tableName, ReportService.class);
        bean.report(tableName, id, action);
        return "OK";
    }

    @PostMapping("/select")
    public Resp<Object> select(@RequestBody ReportParam param) throws Exception {
        Map m = new HashMap<>();
        ReportService bean = SpringUtil.getBean(param.getTableName(), ReportService.class);
        Page result = bean.selectReportRecord(param);
        m.put("pages", result);
        m.put("options", FieldUtils.getDesc(bean.getGenericTypeClass()));
        return Resp.ok(m);
    }

    @PostMapping("/selectAsset")
    public Resp<Object> selectAsset(@RequestBody ReportParam param) {
        ReportService bean = SpringUtil.getBean(param.getTableName(), ReportService.class);
        Page result = bean.selectAsset(param);
        Map m = new HashMap<>();
        m.put("pages", result);
        m.put("options", FieldUtils.getDesc(bean.getGenericTypeClass()));
        return Resp.ok(m);
    }

    @PostMapping("/upload")
    public Resp<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return uploadFileService.upload(file);
    }

}
